//
//  StagePhotoViewController.m
//  觅影
//
//  Created by Zeus on 16/4/13.
//  Copyright © 2016年  陶虹. All rights reserved.
//

#import "StagePhotoViewController.h"
#import "StageCollectionViewCell.h"
#import "PhotoModel.h"
#import <Foundation/Foundation.h>
@interface StagePhotoViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout,SDWebImageManagerDelegate>
@property (nonatomic, strong) UICollectionView *stageCollectionView;
@property (nonatomic, strong) NSMutableArray *photoArray;
@property (nonatomic, strong) UIImage *placeholderImage;
@property (nonatomic, assign) CGFloat height;
@end

@implementation StagePhotoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationController.navigationBar.translucent = NO;
    self.view.backgroundColor = [UIColor colorWithRed:255.0 green:255.0 blue:255.0 alpha:1];
    self.navigationItem.title = @"电影剧照";
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.minimumLineSpacing = 5;
    flowLayout.minimumInteritemSpacing = 10;
    self.stageCollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, KWIDTH, KHEIGHT - 64) collectionViewLayout:flowLayout];
    self.stageCollectionView.delegate = self;
    self.stageCollectionView.dataSource = self;
    
    self.stageCollectionView.backgroundColor = [UIColor whiteColor];
    [self.stageCollectionView registerClass:[StageCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    [self.view addSubview:self.stageCollectionView];
    
    self.photoArray = [NSMutableArray array];
    [RequestManager requestWithUrl:[NSString stringWithFormat:@"http://static.v6v7.com/pingping_japi/movie/stilllist?movieId=%@&page=1&page_size=30", _movieId] requestType:RequestTypeGET parDic:nil finshed:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        self.photoArray = [PhotoModel PhotomodelConfigureJson:dic];
        if (self.photoArray.count == 0) {
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"抱歉，没有相关信息，快去看看别的吧" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil];
            [alert addAction:action];
            [self presentViewController:alert animated:YES completion:nil];
        }
        else
        {
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.stageCollectionView reloadData];
            });
        }
    } error:nil];
}

#pragma mark --- 返回每个分区下的行数 ---
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.photoArray.count;
}

#pragma mark --- 返回cell ---
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    StageCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    PhotoModel *photo = self.photoArray[indexPath.row];
    [cell.picImageView sd_setImageWithURL:[NSURL URLWithString:photo.image_url_spider_small]];
    return cell;
}

#pragma mark --- 返回每个item宽和高 ---
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake((KWIDTH - 40)/3, 110);
}

#pragma mark --- 返回集合视图整体上 左 下 右边距 ---
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(10, 10, 10, 10);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
